package com.falcon.utils;

import com.swan.core.utils.NameUtil;
import org.springframework.util.StringUtils;

import java.io.File;

/** 路径工具类
 * @author zongf
 * @since 2021-03-25
 */
public class PathUtil {

    /** 获取 java 文件绝对路径
     * @return String
     * @author zongf
     * @since 2021-03-01
     */
    public static String getClassPath(String projectPath, String packageName, String fileName) {
        return getPath(projectPath, "src/main/java",packageName, fileName, ".java");
    }

    /** 获取 xml 文件绝对路径
     * @return String
     * @author zongf
     * @since 2021-03-01
     */
    public static String getXmlPath(String projectPath, String packageName, String fileName) {
        return getPath(projectPath,"src/main/resources", packageName, fileName,  ".xml");
    }

    public static String getPath(String projectPath, String relativePath, String fixDir, String fileName, String suffix) {

        // 项目目录
        if (!StringUtils.hasText(projectPath)) {
            String newFilePath = new File("tmp").getAbsolutePath();
            int idx = newFilePath.lastIndexOf("/");
            projectPath = newFilePath.substring(0, idx);
        }

        StringBuffer sb = new StringBuffer();
        sb.append(projectPath)
                .append("/").append(relativePath)
                .append("/").append(fixDir.replaceAll("\\.", "/"))
                .append("/").append(fileName)
                .append(suffix);
        return sb.toString().replaceAll("//", "/");
    }


    public static String handleClassName(String tableName, String[] tablePrefixList) {

        // 处理前缀
        StringBuffer nameSb = new StringBuffer();

        for (String tablePrefix : tablePrefixList) {
            if (StringUtils.startsWithIgnoreCase(tableName, tablePrefix)) {
                int idx = tableName.indexOf(tablePrefix);
                if (idx != -1) {
                    tableName = tableName.substring(idx + tablePrefix.length());
                    break;
                }
            }
        }

        nameSb.append(NameUtil.firstUppercase(NameUtil.toHumpName(tableName)));
        return nameSb.toString();
    }

}
